
package com.hexinfo.dmpro.sparing.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hexinfo.dmpro.common.utils.CommonConstants;
import com.hexinfo.dmpro.common.utils.StringUtils;
import com.hexinfo.dmpro.sparing.model.SyncRuleConf;
import com.hexinfo.dmpro.sparing.model.TableSyncConf;
import com.hexinfo.dmpro.sparing.dao.TableSyncConfMapper;
import com.hexinfo.dmpro.sparing.service.TableSyncConfService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 热备表级同步规则表
 *
 * @author yemw
 * @date 2024-04-26 10:16:47
 */
@Service
public class TableSyncConfServiceImpl extends ServiceImpl<TableSyncConfMapper, TableSyncConf> implements TableSyncConfService {

    @Override
    public Page queryList(Page page, TableSyncConf tableSyncConf) {
        LambdaQueryWrapper<TableSyncConf> wrapper = Wrappers.lambdaQuery();
        wrapper .like(StrUtil.isNotBlank(tableSyncConf.getSourceName())&&!"无源".equals(tableSyncConf.getSourceName()), TableSyncConf::getSourceName, tableSyncConf.getSourceName())
                .isNull("无源".equals(tableSyncConf.getSourceName()),TableSyncConf::getSourceName)
                .like(StrUtil.isNotBlank(tableSyncConf.getTbName()),TableSyncConf::getTbName,tableSyncConf.getTbName())
                .like(StrUtil.isNotBlank(tableSyncConf.getDbName()),TableSyncConf::getDbName,tableSyncConf.getDbName())
                .like(StrUtil.isNotBlank(tableSyncConf.getDbTbName()),TableSyncConf::getDbTbName,tableSyncConf.getDbTbName())
                .like(StrUtil.isNotBlank(tableSyncConf.getDatePartition()),TableSyncConf::getDatePartition,tableSyncConf.getDatePartition())
                .eq(TableSyncConf::getDel,CommonConstants.STATUS_NORMAL)
                .orderByDesc(TableSyncConf::getSourceName);
        Page returnPage = this.baseMapper.selectPage(page, wrapper);
        return returnPage;
    }

    /**
     * 按照源名获取表集合
     * @param sourceName
     * @return
     */
    @Override
    public List<TableSyncConf> queryList(String sourceName) {
        LambdaQueryWrapper<TableSyncConf> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TableSyncConf::getDel,CommonConstants.STATUS_NORMAL);
        wrapper.eq(TableSyncConf::getSourceName,sourceName);
        List<TableSyncConf> tableSyncConfs = this.baseMapper.selectList(wrapper);
        return tableSyncConfs;
    }

    @Override
    public boolean remove(String id) {
        TableSyncConf tableSyncConf = new TableSyncConf();
        tableSyncConf.setId(id);
        tableSyncConf.setDel(CommonConstants.STATUS_DEL);
        int rem = this.baseMapper.updateById(tableSyncConf);
        if(rem>0){
            return true;
        }else{
            return false;
        }
    }
}
